﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqData;
using Cricket.LinqModel;

public partial class Login : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Admin admin;  
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            
            string username = txtUsername.Text;
            string password = txtPassword.Text;
            string password2 = txtRetypePassword.Text;
            admin = new Admin(_dataObject);
            List<SpLoginUserResult> loginUser = new List<SpLoginUserResult>();
            loginUser = admin.GetAdminByCredential(username, password, password2);
            if (loginUser.Count > 0)
            {
                LoggedInUser user = new LoggedInUser();
                user.AdminId = Convert.ToInt32(loginUser[0].Id);
                user.AdminName = loginUser[0].FullName;
                user.BetId = Convert.ToInt32(loginUser[0].BatId);
                user.UserType = Convert.ToInt32(loginUser[0].UserType);
                user.FullName = loginUser[0].FullName;

                HttpCookie cookie = new HttpCookie("Admin");
                //Cookie Data
                cookie["AdminId"] = Convert.ToString(loginUser[0].Id);
                cookie["AdminName"] = loginUser[0].FullName;
                cookie["BetId"] = Convert.ToString(loginUser[0].BatId);
                cookie["UserType"] = Convert.ToString(loginUser[0].UserType);
                cookie["FullName"] = loginUser[0].FullName.ToString();
                cookie["CommonRate"] = loginUser[0].CommonRate.ToString();
                cookie.Expires = DateTime.Now.AddMinutes(15);
                Response.Cookies.Add(cookie);



                if (loginUser[0].IsFirstLogin == true)
                {
                    Response.Redirect("ChangePassword.aspx");
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "call me", "BindPopup()", true);
                    Response.Redirect("Dashboard.aspx");
                }
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
        }
    }
}